package com.newcoin.xgb.controller;

import com.newcoin.xgb.dto.*;
import com.newcoin.xgb.entity.*;
import com.newcoin.xgb.po.ShipAddressPo;
import com.newcoin.xgb.po.TeamMemberPo;
import com.newcoin.xgb.po.TeamReportPo;
import com.newcoin.xgb.service.XgbUserService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

@RestController
@Slf4j
@RequestMapping(value = "/xgb")
@Api(description = "乐刷用户接口")
public class XgbUserController {

    @Autowired
    private XgbUserService service;


    @ApiOperation(value = "注册")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "mobile", value = "手机号", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "invCode", value = "邀请码", required = false, dataType = "String"),
                    @ApiImplicitParam(name = "verCode", value = "验证码", required = true, dataType = "String")
            }
    )
    @PostMapping(value = "/registered")
    public Result registered(@Valid RegisteredDto registeredDto) {
        try {
            return service.registered(registeredDto);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("注册失败");
            return result;
        }
    }


    @ApiOperation(value = "登录")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "mobile", value = "手机号", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String")
            }
    )
    @PostMapping(value = "/login")
    public Result<XgbUsers> login(LoginDto loginDto) {
        try {
            return service.login(loginDto);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result<XgbUsers> result = new Result();
            result.setCode(500);
            result.setMessage("登录失败");
            return result;
        }
    }

    @ApiOperation(value = "退出登录")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String"),
            }
    )
    @PostMapping(value = "/outLogin")
    public Result outLogin(Long userId) {
        try {
            return service.outLogin(userId);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("退出登录失败");
            return result;
        }
    }


    @ApiOperation(value = "修改头像")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "path", value = "文件路径", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String")
            }
    )
    @PostMapping(value = "/changeHead")
    public Result changeHead(String path, Long userId) {
        try {
            return service.changeHead(path, userId);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("修改头像失败");
            return result;
        }
    }

    @ApiOperation(value = "修改昵称")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "nickname", value = "昵称", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String")
            }
    )
    @PostMapping(value = "/changeNickName")
    public Result changeNickName(@Valid ChangeNicknameDto changeNicknameDto) {
        try {
            return service.changeNickName(changeNicknameDto);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("修改昵称失败");
            return result;
        }
    }

    @ApiOperation(value = "修改登陆密码/设置支付密码/修改支付密码")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "newPwd", value = "新密码", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "verCode", value = "验证码", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "mobile", value = "手机号", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "type", value = "修改密码的类型(0 登录密码 1 支付密码)", required = true, dataType = "int")
            }
    )
    @PostMapping(value = "/changePassword")
    public Result changePassword(ChangePasswordDto changePasswordDto) {
        try {
            return service.changePassword(changePasswordDto);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("修改密码失败");
            return result;
        }
    }

    @ApiOperation(value = "忘记密码")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "newPwd", value = "新密码", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "verCode", value = "验证码", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "mobile", value = "手机号", required = true, dataType = "String")
            }
    )
    @PostMapping(value = "/forgetPassword")
    public Result forgetPassword(ForgetPasswordDto forgetPasswordDto) {
        try {
            return service.forgetPassword(forgetPasswordDto);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("重置密码失败");
            return result;
        }
    }


    @ApiOperation(value = "添加收货地址")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "name", value = "真实姓名", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "mobile", value = "手机号", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "postalCode", value = "邮政编码", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "area", value = "地区", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "address", value = "详细地址", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "isDefault", value = "是否为默认地址", required = true, dataType = "boolean")
            }
    )
    @PostMapping(value = "/addShipAddress")
    public Result addShipAddress(AddShipAddressDto addShipAddressDto) {
        try {
            return service.addShipAddress(addShipAddressDto);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("添加收货地址失败");
            return result;
        }
    }

    @ApiOperation(value = "获取收货地址")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "page", value = "查询第几页 (每页10条)", required = true, dataType = "long"),
                    @ApiImplicitParam(name = "", value = "忽略非必填参数", required = false)
            }
    )
    @GetMapping(value = "/getShipAddress")
    public Result<ShipAddressPo> getShipAddress(QueryParameterDto queryParameterDto) {
        try {
            return service.getShipAddress(queryParameterDto);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result<ShipAddressPo> result = new Result();
            result.setCode(500);
            result.setMessage("获取收货地址失败");
            return result;
        }
    }

    @ApiOperation(value = "根据id获取收货地址信息")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "id", value = "银行卡信息id", required = true, dataType = "String"),
            }
    )
    @GetMapping(value = "/getShipAddressById")
    public Result<XgbShipAddress> getShipAddressById(QueryShipAddressById queryShipAddressById) {
        try {
            return service.getShipAddressById(queryShipAddressById);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("获取收货地址信息失败");
            return result;
        }
    }

    @ApiOperation(value = "修改收货地址")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "name", value = "真实姓名", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "mobile", value = "手机号", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "postalCode", value = "邮政编码", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "area", value = "地区", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "address", value = "详细地址", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "isDefault", value = "是否为默认地址", required = true, dataType = "boolean")
            }
    )
    @PostMapping(value = "/changeShipAddress")
    public Result changeShipAddress(ChangeShipAddressDto changeShipAddressDto) {
        try {
            return service.changeShipAddress(changeShipAddressDto);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("修改收货地址失败");
            return result;
        }
    }

    @ApiOperation(value = "删除收货地址")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "id", value = "地址id", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "", value = "忽略非必填参数", required = false)
            }
    )
    @PostMapping(value = "/deleteShipAddress")
    public Result deleteShipAddress(QueryParameterDto queryParameterDto) {
        try {
            return service.deleteShipAddress(queryParameterDto);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("删除收货地址失败");
            return result;
        }
    }


    @ApiOperation(value = "获取团队报表信息")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "", value = "忽略非必填参数", required = false)
            }
    )
    @GetMapping(value = "/getTeamReport")
    public Result<TeamReportPo> getTeamReport(QueryParameterDto queryParameterDto) {
        try {
            return service.getTeamReport(queryParameterDto);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result<TeamReportPo> result = new Result();
            result.setCode(500);
            result.setMessage("获取团队包表信息出错");
            return result;
        }
    }

    @ApiOperation(value = "获取团队列表信息")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "page", value = "页", required = true, dataType = "int", defaultValue = "0"),
                    @ApiImplicitParam(name = "type", value = "代数", required = true, dataType = "int"),
                    @ApiImplicitParam(name = "", value = "忽略非必填参数", required = false)
            }
    )
    @GetMapping(value = "/getTeamMember")
    public Result<TeamMemberPo> getTeamMember(QueryParameterDto queryParameterDto) {
        try {
            return service.getTeamMember(queryParameterDto);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result<TeamMemberPo> result = new Result();
            result.setCode(500);
            result.setMessage("获取团队列表出错");
            return result;
        }
    }


    @ApiOperation(value = "检测是设置支付密码")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "", value = "忽略非必填参数", required = false)
            }
    )
    @GetMapping(value = "/isSettingPayPwd")
    public Result<Boolean> isSettingPayPwd(QueryParameterDto queryParameterDto) {
        try {
            return service.isSettingPayPwd(queryParameterDto);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result<Boolean> result = new Result();
            result.setCode(500);
            result.setMessage("检测是否设置支付密码失败");
            return result;
        }
    }

    @ApiOperation(value = "获取分享海报")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "", value = "忽略非必填参数", required = false)
            }
    )
    @GetMapping(value = "/getInvCodePath")
    public Result<String> getInvCodePath(QueryParameterDto queryParameterDto) {
        try {
            return service.getInvCodePath(queryParameterDto);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result<String> result = new Result();
            result.setCode(500);
            result.setMessage("获取分享海报失败");
            return result;
        }
    }

    @ApiOperation(value = "检测是否添加收货地址")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "", value = "忽略非必填参数", required = false)
            }
    )
    @GetMapping(value = "/isBindReceiptAddress")
    public Result<Boolean> isBindReceiptAddress(QueryParameterDto queryParameterDto) {
        try {
            return service.isBindReceiptAddress(queryParameterDto);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result<Boolean> result = new Result();
            result.setCode(500);
            result.setMessage("检测是否添加收货地址失败");
            return result;
        }
    }

    @ApiOperation(value = "实名认证")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "name", value = "真实姓名", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "idCard", value = "身份证号", required = true, dataType = "String"),
            }
    )
    @PostMapping(value = "/verified")
    public Result verified(VerifiedDto verifiedDto) {
        try {
            return service.verified(verifiedDto);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("实名失败");
            return result;
        }
    }

    @ApiOperation(value = "获取银行信息")
    @GetMapping(value = "/getBankList")
    public Result<List<String>> getBankList() {
        try {
            return service.getBankList();
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("获取失败");
            return result;
        }
    }

    @ApiOperation(value = "获取银行卡列表")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String")
            }
    )
    @GetMapping(value = "/getUserBank")
    public Result<List<XgbUserBank>> getUserBank(Long userId, String token) {
        try {
            return service.getUserBank(userId, token);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("获取失败");
            return result;
        }
    }

    @ApiOperation(value = "解绑银行卡")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String")
            }
    )
    @PostMapping(value = "/UnBundlingBank")
    public Result UnBundlingBank(Long userId, String token, String id) {
        try {
            return service.UnBundlingBank(userId, token, id);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("解绑失败");
            return result;
        }
    }

    @ApiOperation(value = "绑定银行卡")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "openBank", value = "开户行", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "cardholder", value = "持卡人", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "cardNumber", value = "银行卡号", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "openBranchBank", value = "开户支行", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "mobile", value = "手机号", required = true, dataType = "String")
            }
    )
    @PostMapping(value = "/bundlingBank")
    public Result bundlingBank(Long userId, String token, String openBank, String cardholder, String cardNumber, String openBranchBank, String mobile){
        try {
            return service.bundlingBank(userId, token, openBank, cardholder, cardNumber, openBranchBank, mobile);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("绑定失败");
            return result;
        }
    }
}
